# Maintainer: Rafal Brzegowy <rafal.brzegowy@yahoo.com>

_realname=opencascade
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=7.7.0
_pkgver2=V${pkgver//./_}
pkgrel=1
pkgdesc='Open CASCADE Technology, 3D modeling & numerical simulation (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=("${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-freeimage"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-tbb"
         "${MINGW_PACKAGE_PREFIX}-openvr"
         "${MINGW_PACKAGE_PREFIX}-rapidjson"
         "${MINGW_PACKAGE_PREFIX}-tcl"
         "${MINGW_PACKAGE_PREFIX}-tk"
         "${MINGW_PACKAGE_PREFIX}-vtk")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-qt5-base")
license=('spdx:LGPL-2.1-or-later WITH OCCT-exception-1.0')
url='https://www.opencascade.org'
source=("opencascade-${pkgver}.tgz::https://git.dev.opencascade.org/gitweb/?p=occt.git;a=snapshot;h=refs/tags/${_pkgver2};sf=tgz"
        "0001-fix-compile-openvr.patch"

        # The following patches are ported from the Debian Science Team
        # https://salsa.debian.org/science-team/opencascade/-/tree/master/debian/patches

        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0002-GeomPlate_BuildAveragePlane-BasePlan-Don-t-set-yvect.patch
        "0002-GeomPlate_BuildAveragePlane-BasePlan-Don-t-set-yvect.patch"

        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0003-BRepFill_Filling-WireFromList-We-can-t-assume-that-a.patch
        "0003-BRepFill_Filling-WireFromList-We-can-t-assume-that-a.patch"

        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0004-BRepFill_Filling-Curve-constraints-confused-by-impli.patch
        "0004-BRepFill_Filling-Curve-constraints-confused-by-impli.patch"

        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0005-BRepFill_Filling-Don-t-even-attempt-to-build-with-em.patch
        "0005-BRepFill_Filling-Don-t-even-attempt-to-build-with-em.patch"

        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0006-BRepOffset_Tool-TryProject-Check-return-of-BRepLib-B.patch
        "0006-BRepOffset_Tool-TryProject-Check-return-of-BRepLib-B.patch"

        "0007-static-build.patch"
        "0008-clang-dllexport.patch"
        "0009-sse2-detetction-gcc.patch"
        "0010-Fix-TBB-detection.patch"
        "0011-Fix-linking-TKXDE.patch"
        "0012-ViewerTest-include-missing-header.patch"
        "0013-ExpToCasExe-Fix-building-static.patch")

sha256sums=('075ca1dddd9646fcf331a809904925055747a951a6afd07a463369b9b441b445'
            '0bbba5858e3e478759dac3d4802f821ec633304a63aeef542e0e4e0e0674e205'
            '9e72d389359899d959fa66b131c9871c15a136bb600c246fcf7e05910d277335'
            'f126465de547d606c4af2e95bd550ba7d5528d53a9b73693078952f9a40b3cae'
            '1f6062eece494ccec34b2b7fae0004d3970d477835f78a3fbacb0180a60f279f'
            'fa627467f355b541216a90ca9170fb87893652d6608c129bd913b19b7d1c9ee5'
            'd39ce849914e6349161675b9d412351684eb6af4bed1aa31afbdc54fa153a532'
            'c202fa3d21b3342595a15690be9e870488154a37cbfbea0a847fff62b7466f3a'
            '4c9da8d7176813392a1d8a972c88c20f242145fe15e712ea65a104b08ef19b58'
            '3bc45477d80f32626a301fc714e8060e08697ff3fc8ac5c10e3fdc70c77000f0'
            '3d927cd0b2def78db8def263cd40bfc8520b8c8520bf5a7543f0775e02e5e86e'
            '530d03546cb545e7b7290359fa569c0a7902ea5603a50b2834f1136b3949380f'
            '74723f88567285831259cbbb7435a1db82203fd44f305845e2ffb3d5130e7c69'
            '6c1e3cfebecc398424d94de43d501d2718800bf101f90a156c086fad4ba042e0')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  cd "${srcdir}"/occt-${_pkgver2}
  apply_patch_with_msg \
    0001-fix-compile-openvr.patch \
    0002-GeomPlate_BuildAveragePlane-BasePlan-Don-t-set-yvect.patch \
    0003-BRepFill_Filling-WireFromList-We-can-t-assume-that-a.patch \
    0004-BRepFill_Filling-Curve-constraints-confused-by-impli.patch \
    0005-BRepFill_Filling-Don-t-even-attempt-to-build-with-em.patch \
    0006-BRepOffset_Tool-TryProject-Check-return-of-BRepLib-B.patch \
    0007-static-build.patch \
    0008-clang-dllexport.patch \
    0009-sse2-detetction-gcc.patch \
    0010-Fix-TBB-detection.patch \
    0011-Fix-linking-TKXDE.patch \
    0012-ViewerTest-include-missing-header.patch \
    0013-ExpToCasExe-Fix-building-static.patch

  find . -name "*.orig" -exec rm -f {} \;
}

build() {
  local common_config
  common_config=(
    -DUSE_D3D=1
    -D3RDPARTY_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_TK_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_TK_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_TK_DLL="${MINGW_PREFIX}/bin/tk86.dll"
    -D3RDPARTY_TK_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_TCL_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_TCL_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_TCL_DLL="${MINGW_PREFIX}/bin/tcl86.dll"
    -D3RDPARTY_TCL_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_FREETYPE_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_FREETYPE_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FREETYPE_DLL="${MINGW_PREFIX}/bin/libfreetype-6.dll"
    -D3RDPARTY_FREETYPE_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -DUSE_FFMPEG=1
    -D3RDPARTY_FFMPEG_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_FFMPEG_DLL_DIR_avformat="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_avformat="${MINGW_PREFIX}/bin/avformat-58.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_avformat="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_FFMPEG_DLL_DIR_avutil="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_avutil="${MINGW_PREFIX}/bin/avutil-56.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_avutil="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_FFMPEG_DLL_DIR_avcodec="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_avcodec="${MINGW_PREFIX}/bin/avcodec-58.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_avcodec="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_FFMPEG_DLL_DIR_swscale="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_swscale="${MINGW_PREFIX}/bin/swscale-5.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_swscale="${MINGW_PREFIX}/lib"
    -DUSE_OPENVR=1
    -D3RDPARTY_OPENVR_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_OPENVR_DLL_DIR_openvr_api="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_OPENVR_DLL_openvr_api="${MINGW_PREFIX}/bin/libopenvr_api.dll"
    -D3RDPARTY_OPENVR_LIBRARY_DIR_openvr_api="${MINGW_PREFIX}/lib/"
    -DUSE_RAPIDJSON=1
    -D3RDPARTY_RAPIDJSON_DIR="${MINGW_PREFIX}"
    -DUSE_FREEIMAGE=1
    -D3RDPARTY_FREEIMAGE_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_FREEIMAGE_DLL_DIR_freeimage="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FREEIMAGE_DLL_freeimage="${MINGW_PREFIX}/bin/libfreeimage-3.dll"
    -D3RDPARTY_FREEIMAGE_LIBRARY_DIR_freeimage="${MINGW_PREFIX}/lib/"
    -DUSE_TBB=1
    -D3RDPARTY_TBB_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_TBB_DLL="${MINGW_PREFIX}/bin/libtbb12.dll"
    -D3RDPARTY_TBB_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_TBB_LIBRARY="${MINGW_PREFIX}/lib/libtbb12.dll.a"
    -D3RDPARTY_TBBMALLOC_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_TBBMALLOC_DLL="${MINGW_PREFIX}/bin/libtbbmalloc.dll"
    -D3RDPARTY_TBBMALLOC_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_TBBMALLOC_LIBRARY="${MINGW_PREFIX}/lib/libtbbmalloc.dll.a"
    -DUSE_VTK=1
    -D3RDPARTY_VTK_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_VTK_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_VTK_INCLUDE_DIR="${MINGW_PREFIX}/include/vtk"
  )

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-i686 ]]; then
    common_config+=(
      -DBUILD_DOC_Overview=Off
    )
  fi

  msg2 "Building static libraries"
  [[ -d "${srcdir}/build-${MSYSTEM}-static" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-static"
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake -Wno-dev \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -GNinja \
    -DCMAKE_BUILD_TYPE="Release" \
    -DINSTALL_DIR_LAYOUT="Unix" \
    -DBUILD_LIBRARY_TYPE="Static" \
    "${common_config[@]}" \
    -D3RDPARTY_TK_LIBRARY="${MINGW_PREFIX}/lib/libtk86.a" \
    -D3RDPARTY_TCL_LIBRARY="${MINGW_PREFIX}/lib/libtcl86.a" \
    -D3RDPARTY_FREETYPE_LIBRARY="${MINGW_PREFIX}/lib/libfreetype.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avformat="${MINGW_PREFIX}/lib/libavformat.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avutil="${MINGW_PREFIX}/lib/libavutil.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avcodec="${MINGW_PREFIX}/lib/libavcodec.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_swscale="${MINGW_PREFIX}/lib/libswscale.a" \
    -D3RDPARTY_FREEIMAGE_LIBRARY="${MINGW_PREFIX}/lib/libfreeimage.a" \
    -D3RDPARTY_OPENVR_LIBRARY_openvr_api="${MINGW_PREFIX}/lib/libopenvr_api.a" \
    -DOPENVDB_USE_STATIC_LIBS=ON \
    ../occt-${_pkgver2}

  ${MINGW_PREFIX}/bin/cmake --build .

  msg2 "Building shared libraries"
  [[ -d "${srcdir}/build-${MSYSTEM}-shared" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-shared"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -GNinja \
    -DCMAKE_BUILD_TYPE="Release" \
    -DINSTALL_DIR_LAYOUT="Unix" \
    -DBUILD_LIBRARY_TYPE="Shared" \
    "${common_config[@]}" \
    -D3RDPARTY_TK_LIBRARY="${MINGW_PREFIX}/lib/libtk86.dll.a" \
    -D3RDPARTY_TCL_LIBRARY="${MINGW_PREFIX}/lib/libtcl86.dll.a" \
    -D3RDPARTY_FREETYPE_LIBRARY="${MINGW_PREFIX}/lib/libfreetype.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avformat="${MINGW_PREFIX}/lib/libavformat.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avutil="${MINGW_PREFIX}/lib/libavutil.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avcodec="${MINGW_PREFIX}/lib/libavcodec.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_swscale="${MINGW_PREFIX}/lib/libswscale.dll.a" \
    -D3RDPARTY_FREEIMAGE_LIBRARY="${MINGW_PREFIX}/lib/libfreeimage.dll.a" \
    -D3RDPARTY_OPENVR_LIBRARY_openvr_api="${MINGW_PREFIX}/lib/libopenvr_api.dll.a" \
    ../occt-${_pkgver2}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  # Static Install
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install

  # Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install

  install -Dm644 "${srcdir}/occt-${_pkgver2}/LICENSE_LGPL_21.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE_LGPL_21.txt"
  install -Dm644 "${srcdir}/occt-${_pkgver2}/OCCT_LGPL_EXCEPTION.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/OCCT_LGPL_EXCEPTION.txt"

  # Fix cmake files
  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f \( -name '*.cmake' \) \
      -exec sed -i -e 's|\${OCCT_INSTALL_BIN_LETTER}||g' {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f \( -name '*.cmake' \) \
      -exec sed -i -e "s|$(cygpath -m "${MINGW_PREFIX}")/lib/||g" {} \;
}
